package com.hangwei.paperless.adapter.sub;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseViewHolder;
import com.hangwei.paperless.R;
import com.hangwei.paperless.model.bean.PartCheckInfo;
import com.hangwei.paperless.view.SealHelper;
import com.twelve.all.adapter.MyAdapter;
import com.twelve.all.utils.Tools;

import java.util.List;

/**
 * Created by 张龙臣 on 2020/12/1.
 */

public class SubPartCheckAdapter extends MyAdapter<PartCheckInfo, BaseViewHolder> {
    /**
     * 整个被NA了就不需要
     */
    private boolean mNeedNa;

    public SubPartCheckAdapter(@Nullable List<PartCheckInfo> data) {
        super(R.layout.item_sub_part_check, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, PartCheckInfo item) {

        if (item.isNA) {
            helper.setBackgroundColor(R.id.llItemView, mContext.getResources().getColor(R.color.lightGray))
                    .setGone(R.id.tvNa, true)
                    .setGone(R.id.ivObliqueLine, true)
                    .setText(R.id.tvNa, Tools.checkedString(item.naTypeCode));
        } else {
            helper.setGone(R.id.tvNa, false)
                    .setGone(R.id.ivObliqueLine, false);
            if (item.isComplete)
                helper.setBackgroundColor(R.id.llItemView, mContext.getResources().getColor(R.color.complete));
            else {
                if (item.disposition == null || item.disposition.size() == 0) {
                    helper.setBackgroundColor(R.id.llItemView, mContext.getResources().getColor(R.color.white));
                } else {
                    if (!item.disposition.contains("合格"))
                        helper.setBackgroundColor(R.id.llItemView, mContext.getResources().getColor(R.color.inProgress));
                    else
                        helper.setBackgroundColor(R.id.llItemView, mContext.getResources().getColor(R.color.white));
                }
            }
        }
        if (!mNeedNa){
            helper.setBackgroundColor(R.id.llItemView, mContext.getResources().getColor(R.color.lightGray));
        }
        helper.setGone(R.id.ivNa, mNeedNa && !item.isAdd && (item.disposition == null || item.disposition.size() == 0) && !item.isNA);

        StringBuilder builder = new StringBuilder();
        if (item.disposition != null && item.disposition.size() > 0) {
            for (int i = 0; i < item.disposition.size(); i++) {
                builder.append(item.disposition.get(i));
                if (i != item.disposition.size() - 1)
                    builder.append(",");
            }
        }
        helper.addOnClickListener(R.id.ivEdit)
                .addOnClickListener(R.id.ivNa)
                .addOnClickListener(R.id.llItemView)
                .setText(R.id.tvPowNo, Tools.checkedString(item.seq))
                .setText(R.id.tvSeq, Tools.checkedString(item.location))
                .setText(R.id.tvPartCheck, Tools.checkedString(item.checkItem))
                .setText(R.id.tvDesignMin, TextUtils.isEmpty(Tools.checkedString(item.dlMin)) ? "--" : Tools.checkedString(item.dlMin))
                .setText(R.id.tvDesignMax, TextUtils.isEmpty(Tools.checkedString(item.dlMax)) ? "--" : Tools.checkedString(item.dlMax))
                .setText(R.id.tvReworkMin, TextUtils.isEmpty(Tools.checkedString(item.rlMin)) ? "--" : Tools.checkedString(item.rlMin))
                .setText(R.id.tvReworkMax, TextUtils.isEmpty(Tools.checkedString(item.rlMax)) ? "--" : Tools.checkedString(item.rlMax))
                .setText(R.id.tvActual, TextUtils.isEmpty(Tools.checkedString(item.actual)) ? "--" : Tools.checkedString(item.actual))
                .setText(R.id.tvRemark, Tools.checkedString(item.remark))
                .setText(R.id.tvDisposition, builder.toString())
                .setText(R.id.tvDIM, TextUtils.isEmpty(Tools.checkedString(item.dimAfterRework)) ? "--" : Tools.checkedString(item.dimAfterRework))
                .setText(R.id.tvGrind, TextUtils.isEmpty(Tools.checkedString(item.dimAfterGrind)) ? "--" : Tools.checkedString(item.dimAfterGrind));
//                .setText(R.id.flTName, Tools.checkedString(item.seq))
        ImageView ivEdit = helper.getView(R.id.ivEdit);
        if (item.isAdd) {
            if (TextUtils.isEmpty(item.grindTechId) && TextUtils.isEmpty(item.technicianId)) {
                ivEdit.setImageResource(R.drawable.delete);
                ivEdit.setVisibility(View.VISIBLE);
            } else ivEdit.setVisibility(View.GONE);
        } else {
            ivEdit.setImageResource(R.drawable.add_line);
            ivEdit.setVisibility(View.VISIBLE);
        }

        RelativeLayout flTName = helper.getView(R.id.flTName);
        RelativeLayout flUserSeal = helper.getView(R.id.flUserSeal);
//        if (TextUtils.isEmpty(item.grindRoleId)) {
//            flTName.removeAllViews();
//        } else {
//        }
//        flTName.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        SealHelper sealHelper = new SealHelper(mContext) {
            @Override
            protected void onSeal() {

            }
        };
        sealHelper.bindData(new SealHelper.SealBean(true, item.grindTechStampId, item.grindTechId, item.grindTechStampTypeCode, item.grindTechStampCode, item.grindTechDate, item.grindTech, false));
        flTName.removeAllViews();
        flTName.addView(sealHelper.getItemView());
        //去除印章背景
//        if (item.isComplete)
//            helper.setBackgroundColor(R.id.flTName, mContext.getResources().getColor(R.color.complete));
//        else
//            helper.setBackgroundColor(R.id.flTName, mContext.getResources().getColor(R.color.white));
//        if (TextUtils.isEmpty(item.reworkRoleId)) {
//            flUserSeal.removeAllViews();
//        } else {
//        }
        SealHelper sealHelper1 = new SealHelper(mContext) {
            @Override
            protected void onSeal() {

            }
        };
        sealHelper1.bindData(new SealHelper.SealBean(true, item.technicianStampId, item.technicianId, item.technicianStampTypeCode, item.technicianStampCode, item.technicianDate, item.technician, false));
        flUserSeal.removeAllViews();
        flUserSeal.addView(sealHelper1.getItemView());
        //去除印章背景
//        if (item.isComplete)
//            helper.setBackgroundColor(R.id.flUserSeal, mContext.getResources().getColor(R.color.complete));
//        else
//            helper.setBackgroundColor(R.id.flUserSeal, mContext.getResources().getColor(R.color.white));
    }

    public void setNeedNa(boolean needNa) {
        mNeedNa = needNa;
    }
}
